package com.foreveredu.propms.web.struts.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.News;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.NewsActionForm;

public class NewsAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 通过ID查找news详情
	public ActionForward findNewsById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsById(newsform.getId());
		request.setAttribute("news", news);
		news.setTotal(news.getTotal()+1);
		managementService.updateNews(news);
		return mapping.findForward("findNewsById");
	}

	//News 翻页列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if(offset < 0){
			return mapping.findForward("newsList");
		}
		PageModel pm = managementService.findNewsList(offset ,newsform.getBelong());
		request.setAttribute("pm", pm);
		if (newsform.getBelong() == 1) {
			return mapping.findForward("newsList");
		}	
		if (newsform.getBelong() == 2) {
			return mapping.findForward("zcfgList");
		}
		return null;
	}
	
	public ActionForward findNewsByBelong(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsActionForm newsform = (NewsActionForm) form;
		News news = managementService.findNewsByBelong(newsform.getBelong());
		request.setAttribute("news", news);
		if (news.getBelong() == 3) {
			return mapping.findForward("findNewsByBelong3");
		}
		if (news.getBelong() == 4) {
			return mapping.findForward("findNewsByBelong4");
		}
		return null;
	}
}
